{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "187b75c1-a5e8-4444-aa8a-fac22f0cafe4",
   "metadata": {},
   "source": [
    "# 使用前须知\n",
    "使用 nmc_met_io 读取天擎数据库，在配置文件中设置地址和用户信息(若不需要, 则相应项无需配置).\n",
    "\n",
    "在系统用户目录下(\"C:\\Users\\用户名\"(windows)或\"/home/用户名/\"(Linux)), 建立文件夹\".nmcdev\"(若Windows下无法直接创建, 在命令窗口中输入mkdir .nmcdev创建)\n",
    "在\".nmcdev\"中创建文本文件\"config.ini\", 内容模板为:\n",
    "\n",
    "```\n",
    "# 用于nmc_met_io读取大数据云, MICAPS服务器等的配置文件.\n",
    "# 若用不到某个服务器, 则不设置或删除改段落即可.\n",
    "# 注意设置IP地址时, 不要加http等前缀信息.\n",
    "\n",
    "# 当读取CMADaas, MICAPS Cassandra等数据时, 采取了本地文件缓存机制, 以便加快\n",
    "# 文件的读取速度和减少数据库访问次数(设置函数参数cache=False可以不用缓存机制).\n",
    "# CACHE_DIR, 缓存目录, 若没有设置, 默认为 /user_home/.nmcdev/cache\n",
    "# CACHE_DAYS, 缓存天数, 若没有设置, 默认为7, 即7天之前的缓存数据将被删除\n",
    "[CACHE]\n",
    "# CACHE_DIR = ~\n",
    "CACHE_DAYS = 0\n",
    "\n",
    "# CMADaaS大数据云平台配置:\n",
    "#     DNS为IP地址, PORT为端口\n",
    "#     USER_ID和PASSWORD分别为用户名和密码\n",
    "#     serviceNodeId为服务节点名称(一般为 NMIC_MUSIC_CMADAAS)\n",
    "[CMADaaS]\n",
    "DNS = 101.35.88.42\n",
    "PORT = 8000\n",
    "USER_ID = heywhale\n",
    "PASSWORD = DayDayUp\n",
    "serviceNodeId = NMIC_MUSIC_CMADAAS\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d94d08c1-ed0f-4eab-a45e-699209811715",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "from datetime import datetime,timedelta\n",
    "import json\n",
    "import math\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import dateutil.parser\n",
    "import dateutil.rrule\n",
    "import nmc_met_io\n",
    "import nmc_met_io.retrieve_cmadaas\n",
    "import nmc_met_io.retrieve_cmadaas_history\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edf221ce-4b8b-48c0-b7cb-0078ca77dd0b",
   "metadata": {},
   "source": [
    "# 地面观测数据集\n",
    "\n",
    "常用的站点观测数据集有:\n",
    "\n",
    "- SURF_CHN_MUL_MIN, 中国地面分钟数据\n",
    "- SURF_CHN_MUL_HOR, 中国地面逐小时资料\n",
    "- SURF_CHN_MUL_HOR_N, 中国地面逐小时(国家站)\n",
    "- SURF_CHN_MUL_DAY, 中国地面日值数据\n",
    "- SURF_CHN_MUL_DAY_N, 中国地面日值数据(国家站)\n",
    "\n",
    "以上数据集在模拟天擎中只实现了以下接口\n",
    "\n",
    "- getSurfEleInRegionByTime(按时间、地区检索地面要素数据)\n",
    "- getSurfEleInRectByTime(按时间、经纬度范围检索地面数据要素)\n",
    "- getSurfEleByTimeAndStaID(按时间、站号检索地面数据要素)\n",
    "- getSurfEleByTimeRangeAndStaID(按时间段、站号检索地面数据要素)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f3a65bf-1c9a-42e0-b93d-273ada26895b",
   "metadata": {},
   "source": [
    "# 读取单站观测数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "30daec88-5d46-4593-8911-39a4ab7eee19",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set retrieve parameters\n",
    "data_code = \"SURF_CHN_MUL_HOR_N\"  # 中国地面逐小时(国家站)\n",
    "time_range = \"[20211101000000,20211130230000]\"\n",
    "elements = \"Station_Id_C,Lat,Lon,Alti,Year,Mon,Day,Hour,PRS_sea,TEM,DPT,RHU,PRE_1h,PRE_12h,PRE_24h\"\n",
    "sta_ids = \"53691\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0e298a66-d21c-440e-8c2a-c71acd640f15",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data = nmc_met_io.retrieve_cmadaas.cmadaas_obs_by_time_range_and_id(\n",
    "    time_range,\n",
    "    data_code=data_code, \n",
    "    elements=elements, \n",
    "    sta_ids=sta_ids\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d2ede590-92cb-4dd5-8a3f-fb0cb5cc3b95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Station_Id_C</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Alti</th>\n",
       "      <th>Year</th>\n",
       "      <th>Mon</th>\n",
       "      <th>Day</th>\n",
       "      <th>Hour</th>\n",
       "      <th>PRS_sea</th>\n",
       "      <th>TEM</th>\n",
       "      <th>DPT</th>\n",
       "      <th>RHU</th>\n",
       "      <th>PRE_1h</th>\n",
       "      <th>PRE_12h</th>\n",
       "      <th>PRE_24h</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.6</td>\n",
       "      <td>-0.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>0.8</td>\n",
       "      <td>-0.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>-1.1</td>\n",
       "      <td>0.7</td>\n",
       "      <td>-1.5</td>\n",
       "      <td>0.8</td>\n",
       "      <td>-0.8</td>\n",
       "      <td>-0.1</td>\n",
       "      <td>0.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.3</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-0.3</td>\n",
       "      <td>-0.7</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>1.8</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1.1</td>\n",
       "      <td>-1.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>-1.1</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>-0.1</td>\n",
       "      <td>-0.6</td>\n",
       "      <td>1.1</td>\n",
       "      <td>0.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>715</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>19</td>\n",
       "      <td>1.6</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>1.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>-0.8</td>\n",
       "      <td>0.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>716</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>20</td>\n",
       "      <td>0.4</td>\n",
       "      <td>-0.3</td>\n",
       "      <td>-0.9</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.6</td>\n",
       "      <td>1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>717</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>21</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.3</td>\n",
       "      <td>1.1</td>\n",
       "      <td>-0.4</td>\n",
       "      <td>0.7</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>718</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>22</td>\n",
       "      <td>0.9</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.4</td>\n",
       "      <td>-0.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>719</th>\n",
       "      <td>53691</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>23</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>-1.2</td>\n",
       "      <td>0.4</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>-0.4</td>\n",
       "      <td>0.4</td>\n",
       "      <td>1.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>720 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Station_Id_C    Lat     Lon  Alti  Year  Mon  Day  Hour  PRS_sea  TEM  \\\n",
       "0           53691  38.15  114.57   0.0  2021   11    1     0      0.0 -1.6   \n",
       "1           53691  38.15  114.57   0.0  2021   11    1     1     -1.1  0.7   \n",
       "2           53691  38.15  114.57   0.0  2021   11    1     2      0.3 -1.0   \n",
       "3           53691  38.15  114.57   0.0  2021   11    1     3      1.1 -1.9   \n",
       "4           53691  38.15  114.57   0.0  2021   11    1     4     -0.1 -0.6   \n",
       "..            ...    ...     ...   ...   ...  ...  ...   ...      ...  ...   \n",
       "715         53691  38.15  114.57   0.0  2021   11   30    19      1.6  0.0   \n",
       "716         53691  38.15  114.57   0.0  2021   11   30    20      0.4 -0.3   \n",
       "717         53691  38.15  114.57   0.0  2021   11   30    21      1.3  0.3   \n",
       "718         53691  38.15  114.57   0.0  2021   11   30    22      0.9 -0.5   \n",
       "719         53691  38.15  114.57   0.0  2021   11   30    23     -0.5 -1.2   \n",
       "\n",
       "     DPT  RHU  PRE_1h  PRE_12h  PRE_24h  \n",
       "0   -0.8  0.0    -0.2      0.8     -0.7  \n",
       "1   -1.5  0.8    -0.8     -0.1      0.4  \n",
       "2   -0.3 -0.7    -0.5      1.8      0.8  \n",
       "3    0.0  0.3    -1.1      0.6      0.8  \n",
       "4    1.1  0.2     2.0      0.3      0.9  \n",
       "..   ...  ...     ...      ...      ...  \n",
       "715  0.3  1.1     1.5     -0.8      0.6  \n",
       "716 -0.9  0.3     0.1      0.6      1.2  \n",
       "717  1.1 -0.4     0.7     -0.5      0.8  \n",
       "718 -0.2  0.6     0.3      0.4     -0.4  \n",
       "719  0.4 -0.2    -0.4      0.4      1.2  \n",
       "\n",
       "[720 rows x 15 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc5b6fbf-a95c-4064-96d9-2c126276875a",
   "metadata": {},
   "source": [
    "如果需要读取比较长历史的观测记录, 由于大数据云平台存在下载数量限制, 可使用retrieve_cmadass_history模块中的函数逐年下载并拼接."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bbe17f06-6c8a-438a-a38f-f958247dfef1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set retrieve parameters\n",
    "years = np.arange(2000, 2011,1)\n",
    "data_code = \"SURF_CHN_MUL_HOR_N\"    # 逐小时观测资料\n",
    "elements = 'Station_Id_d,Datetime,Lat,Lon,Alti,TEM,DPT,RHU,PRE_1h,PRE_6h,PRE_24h'\n",
    "sta_ids = \"53691\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d9a964c0-ec4b-4992-8cc3-384ccb59e81c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Years: 100%|███████████████████████████████████████████████████████████████████████████| 11/11 [00:06<00:00,  1.79it/s]\n"
     ]
    }
   ],
   "source": [
    "# retrieve data\n",
    "data = nmc_met_io.retrieve_cmadaas_history.get_hist_obs_id(years=years, data_code=data_code, elements=elements, sta_ids=sta_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ff10588b-3753-4538-b1ac-1ffa6fa606cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Station_Id_d</th>\n",
       "      <th>Datetime</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Alti</th>\n",
       "      <th>TEM</th>\n",
       "      <th>DPT</th>\n",
       "      <th>RHU</th>\n",
       "      <th>PRE_1h</th>\n",
       "      <th>PRE_6h</th>\n",
       "      <th>PRE_24h</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.6</td>\n",
       "      <td>2000-01-01 00:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>-0.1</td>\n",
       "      <td>-2.8</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.9</td>\n",
       "      <td>2000-01-01 01:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>-0.9</td>\n",
       "      <td>0.1</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>-0.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.9</td>\n",
       "      <td>2000-01-01 02:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>-1.1</td>\n",
       "      <td>-0.1</td>\n",
       "      <td>0.2</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>-0.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.9</td>\n",
       "      <td>2000-01-01 03:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.8</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>0.6</td>\n",
       "      <td>-2.3</td>\n",
       "      <td>0.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>2000-01-01 04:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.8</td>\n",
       "      <td>1.5</td>\n",
       "      <td>-0.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7915</th>\n",
       "      <td>0.5</td>\n",
       "      <td>2010-01-30 19:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>1.1</td>\n",
       "      <td>-1.8</td>\n",
       "      <td>0.7</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>1.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7916</th>\n",
       "      <td>0.5</td>\n",
       "      <td>2010-01-30 20:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.7</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.4</td>\n",
       "      <td>-0.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7917</th>\n",
       "      <td>1.7</td>\n",
       "      <td>2010-01-30 21:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.7</td>\n",
       "      <td>-0.9</td>\n",
       "      <td>-1.7</td>\n",
       "      <td>-0.6</td>\n",
       "      <td>1.5</td>\n",
       "      <td>-1.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7918</th>\n",
       "      <td>0.6</td>\n",
       "      <td>2010-01-30 22:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>-1.5</td>\n",
       "      <td>-1.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.9</td>\n",
       "      <td>1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7919</th>\n",
       "      <td>2.2</td>\n",
       "      <td>2010-01-30 23:00:00</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.1</td>\n",
       "      <td>-1.6</td>\n",
       "      <td>0.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7920 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Station_Id_d            Datetime    Lat     Lon  Alti  TEM  DPT  RHU  \\\n",
       "0             -1.6 2000-01-01 00:00:00  38.15  114.57   0.0  0.5 -0.1 -2.8   \n",
       "1             -0.9 2000-01-01 01:00:00  38.15  114.57   0.0 -0.5 -0.9  0.1   \n",
       "2             -0.9 2000-01-01 02:00:00  38.15  114.57   0.0  0.8 -1.1 -0.1   \n",
       "3              0.9 2000-01-01 03:00:00  38.15  114.57   0.0  0.1  0.8 -0.2   \n",
       "4              0.0 2000-01-01 04:00:00  38.15  114.57   0.0  1.8  1.5 -0.8   \n",
       "...            ...                 ...    ...     ...   ...  ...  ...  ...   \n",
       "7915           0.5 2010-01-30 19:00:00  38.15  114.57   0.0 -0.5  1.1 -1.8   \n",
       "7916           0.5 2010-01-30 20:00:00  38.15  114.57   0.0  0.7 -0.2  1.3   \n",
       "7917           1.7 2010-01-30 21:00:00  38.15  114.57   0.0 -1.7 -0.9 -1.7   \n",
       "7918           0.6 2010-01-30 22:00:00  38.15  114.57   0.0  0.5 -1.5 -1.4   \n",
       "7919           2.2 2010-01-30 23:00:00  38.15  114.57   0.0  0.1  1.0  1.5   \n",
       "\n",
       "      PRE_1h  PRE_6h  PRE_24h  \n",
       "0       -0.2     0.7      0.4  \n",
       "1       -1.0    -0.2     -0.9  \n",
       "2        0.2    -0.5     -0.3  \n",
       "3        0.6    -2.3      0.7  \n",
       "4        0.0     1.0     -1.4  \n",
       "...      ...     ...      ...  \n",
       "7915     0.7    -0.2      1.3  \n",
       "7916     0.6     0.4     -0.6  \n",
       "7917    -0.6     1.5     -1.3  \n",
       "7918     0.0     0.9      1.2  \n",
       "7919     0.1    -1.6      0.7  \n",
       "\n",
       "[7920 rows x 11 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80e04a40-26e4-46ca-8efd-5a97fe903e1b",
   "metadata": {},
   "source": [
    "# 读取单站的逐日观测数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "41d26b68-8c79-4bfd-af01-94555050313d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set retrieve parameters\n",
    "years = np.arange(2000, 2011,1)\n",
    "data_code = \"SURF_CHN_MUL_DAY\"    # 逐小时观测资料\n",
    "elements = 'Station_Id_d,Datetime,Lat,Lon,Alti,TEM_Avg,TEM_Max,TEM_Min,PRE_Time_2020,PRE_Time_0808,WIN_S_2mi_Avg,WIN_S_Max'\n",
    "sta_ids = \"53691\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "91f95094-c3d2-4eef-8d83-5b8936b09207",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Years: 100%|███████████████████████████████████████████████████████████████████████████| 11/11 [00:01<00:00,  7.15it/s]\n"
     ]
    }
   ],
   "source": [
    "# retrieve data\n",
    "data = nmc_met_io.retrieve_cmadaas_history.get_hist_obs_id(years=years, data_code=data_code, elements=elements, sta_ids=sta_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "14fae239-696a-4ad5-8156-80d2c54029a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Station_Id_d</th>\n",
       "      <th>Datetime</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Alti</th>\n",
       "      <th>TEM_Avg</th>\n",
       "      <th>TEM_Max</th>\n",
       "      <th>TEM_Min</th>\n",
       "      <th>PRE_Time_2020</th>\n",
       "      <th>PRE_Time_0808</th>\n",
       "      <th>WIN_S_2mi_Avg</th>\n",
       "      <th>WIN_S_Max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.6</td>\n",
       "      <td>2000-01-01</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>-2.1</td>\n",
       "      <td>0.2</td>\n",
       "      <td>-0.2</td>\n",
       "      <td>0.8</td>\n",
       "      <td>-0.9</td>\n",
       "      <td>0.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.9</td>\n",
       "      <td>2000-01-02</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.6</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.1</td>\n",
       "      <td>-2.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>-0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.3</td>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.8</td>\n",
       "      <td>0.8</td>\n",
       "      <td>-0.4</td>\n",
       "      <td>1.3</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>-0.3</td>\n",
       "      <td>-0.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.3</td>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.8</td>\n",
       "      <td>0.6</td>\n",
       "      <td>-0.8</td>\n",
       "      <td>0.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.6</td>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.3</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.1</td>\n",
       "      <td>-0.6</td>\n",
       "      <td>-0.8</td>\n",
       "      <td>0.4</td>\n",
       "      <td>1.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1095</th>\n",
       "      <td>0.6</td>\n",
       "      <td>2010-04-06</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.6</td>\n",
       "      <td>-1.3</td>\n",
       "      <td>-1.5</td>\n",
       "      <td>-0.1</td>\n",
       "      <td>0.4</td>\n",
       "      <td>-1.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1096</th>\n",
       "      <td>-1.1</td>\n",
       "      <td>2010-04-07</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.8</td>\n",
       "      <td>-1.6</td>\n",
       "      <td>0.4</td>\n",
       "      <td>-0.1</td>\n",
       "      <td>-2.1</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1097</th>\n",
       "      <td>0.8</td>\n",
       "      <td>2010-04-08</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.8</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.3</td>\n",
       "      <td>-0.4</td>\n",
       "      <td>0.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1098</th>\n",
       "      <td>-1.3</td>\n",
       "      <td>2010-04-09</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.3</td>\n",
       "      <td>-1.4</td>\n",
       "      <td>-1.7</td>\n",
       "      <td>0.3</td>\n",
       "      <td>-1.1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1099</th>\n",
       "      <td>0.7</td>\n",
       "      <td>2010-04-10</td>\n",
       "      <td>38.15</td>\n",
       "      <td>114.57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.6</td>\n",
       "      <td>-0.4</td>\n",
       "      <td>1.3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1100 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Station_Id_d   Datetime    Lat     Lon  Alti  TEM_Avg  TEM_Max  TEM_Min  \\\n",
       "0              0.6 2000-01-01  38.15  114.57   0.0      0.8     -2.1      0.2   \n",
       "1              0.9 2000-01-02  38.15  114.57   0.0     -0.6      1.0      1.3   \n",
       "2              0.3 2000-01-03  38.15  114.57   0.0      1.8      0.8     -0.4   \n",
       "3             -0.3 2000-01-04  38.15  114.57   0.0      0.7      0.1      0.0   \n",
       "4              0.6 2000-01-05  38.15  114.57   0.0     -0.3      0.3      0.1   \n",
       "...            ...        ...    ...     ...   ...      ...      ...      ...   \n",
       "1095           0.6 2010-04-06  38.15  114.57   0.0     -1.0      0.6     -1.3   \n",
       "1096          -1.1 2010-04-07  38.15  114.57   0.0      0.3      0.8     -1.6   \n",
       "1097           0.8 2010-04-08  38.15  114.57   0.0      0.0      0.0     -0.8   \n",
       "1098          -1.3 2010-04-09  38.15  114.57   0.0     -1.3     -1.4     -1.7   \n",
       "1099           0.7 2010-04-10  38.15  114.57   0.0      0.8      0.6      0.8   \n",
       "\n",
       "      PRE_Time_2020  PRE_Time_0808  WIN_S_2mi_Avg  WIN_S_Max  \n",
       "0              -0.2            0.8           -0.9        0.9  \n",
       "1               0.1           -2.5            0.2       -0.5  \n",
       "2               1.3           -0.5           -0.3       -0.4  \n",
       "3              -0.8            0.6           -0.8        0.9  \n",
       "4              -0.6           -0.8            0.4        1.1  \n",
       "...             ...            ...            ...        ...  \n",
       "1095           -1.5           -0.1            0.4       -1.8  \n",
       "1096            0.4           -0.1           -2.1        0.8  \n",
       "1097            1.0           -0.3           -0.4        0.1  \n",
       "1098            0.3           -1.1            1.0       -1.0  \n",
       "1099            0.3            0.6           -0.4        1.3  \n",
       "\n",
       "[1100 rows x 12 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16a08dbe-0942-4060-bb65-b771ec004619",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e84f77e2-e68f-43e0-b412-bcda7e244111",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2224db27-dc76-46d6-acbd-08a43a37525e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21ccb623-8d0d-4554-815f-e19c601b3d76",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "474893af-a0de-4897-95f8-3937a0d98f8f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "170266dc-cc9f-41bc-9769-1a5948665b6b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:webdev]",
   "language": "python",
   "name": "conda-env-webdev-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
